<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        margin: 0 auto;
        width: 500px;
      }
      p {
        display: flex;
        height: 60px;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <p>
        <input id="inp" type="text" value="1,2,4" />
        <span>输入数字求和，数字之间用半角","号分隔</span>
      </p>
      <p>
        <button id="btn">求和</button>
        <span id="sum"></span>
      </p>
    </div>
  </body>
</html>
<script>
  var inp = document.getElementById("inp");
  var btn = document.getElementById("btn");
  var sum = document.getElementById("sum");
  btn.onclick = function () {
    var srr = inp.value.split(",");
    var o = 0;
    for (let i = 0; i < srr.length; i++) {
      o += parseInt(srr[i]);
    }
    sum.innerText = o;
  };
</script>
